﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using Web.Codes;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class PharaphernaliaEntry : BasePage
    {
        [Inject]
        public IPharaphernaliaService _pharaphernaliaService { get; set; }

        public bool IsSuccess { get; set; }
        public string NotifyText { get; set; }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _pharaphernaliaService.GetAll();
            repeater.DataBind();
        }
        void Delete(int id)
        {
            _pharaphernaliaService.Delete(id);
            IsSuccess = _pharaphernaliaService.Save();
            NotifyText = "Successfully Deleted.";
            BindGrid();
        }

        void Edit(int id)
        {
            var phara = _pharaphernaliaService.GetById(id);
            if (phara == null)
                return;

            ObjectToData(phara);

            PharaId.Value = phara.Id.ToString();
            SubmitBtn.Text = "Update";
        }

        void Update(int id)
        {
            var phara = _pharaphernaliaService.GetById(id);
            if (phara == null)
            {
                Add();
                return;
            }
            DataToObject(ref phara);

            _pharaphernaliaService.Update(phara);
            IsSuccess = _pharaphernaliaService.Save();
            NotifyText = "Successfully Updated Record.";
        }
        void Add()
        {
            var phara = new Pharaphernalia();

            DataToObject(ref phara);

            _pharaphernaliaService.Add(phara);
            IsSuccess = _pharaphernaliaService.Save();

            PharaId.Value = phara.Id.ToString();
            NotifyText = "Successfully Added New Record.";
            SubmitBtn.Text = "Update";

        }
        void ClearForm()
        {
            ClearControl(this.Page);
            PharaId.Value = "0";
            SubmitBtn.Text = "Submit";
        }
        void ClearControl(Control ctrl)
        {
            foreach (Control _ctrl in ctrl.Controls)
            {
                if (_ctrl.GetType() == typeof(TextBox))
                {
                    ((TextBox)_ctrl).Text = string.Empty;
                }
                else if (_ctrl.GetType() == typeof(DropDownList))
                {
                    ((DropDownList)_ctrl).SelectedIndex = 0;
                }
                ClearControl(_ctrl);
            }
        }
        void Save()
        {
            int empId;
            int.TryParse(PharaId.Value, out empId);
            if (empId == 0)
                Add();
            else
                Update(empId);
        }
        void DataToObject(ref Pharaphernalia obj)
        {
            double amount;
            double.TryParse(AmountTxt.Text, out amount);
            obj.Description = DescTxt.Text;            
            obj.Amount = amount;
       }

        void ObjectToData(Pharaphernalia obj)
        {
            DescTxt.Text = obj.Description;
            AmountTxt.Text = obj.Amount.ToString();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindGrid();
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Update_Command:
                        Edit(intTemp);
                        break;
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }

        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            Save();
            if (IsSuccess)
            {
                ClearForm();
                BindGrid();
            }
        }

        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            ClearForm();
        }
    }
}